package com.hfdg.dictionary.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.dictionary.mapper.DictionaryMapper;
import com.hfdg.dictionary.entity.Dictionary;
import com.hfdg.dictionary.service.DictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @ClassName DictionaryServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    DictionaryMapper dictionaryMapper;

    @Override
    public String insert(Dictionary dictionary) {
        String uid = UUID.randomUUID().toString();
        dictionary.setUid(uid);
        dictionary.setDelFlag("0");
        dictionary.setCreateTime(new Date());
        dictionaryMapper.insert(dictionary);
        return uid;
    }

    @Override
    public Integer update(Dictionary dictionary) {
        dictionary.setUpdateTime(new Date());
        return dictionaryMapper.update(dictionary);
    }

    @Override
    public Integer  delete(String uid) {
        return dictionaryMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return dictionaryMapper.batchDelete(uids);
    }

    @Override
    public Dictionary get(String uid) {
        return dictionaryMapper.get(uid);
    }

    @Override
    public List<Dictionary> list(Dictionary dictionary) {
    return  dictionaryMapper.list(dictionary);
    }

    @Override
    public PageInfo<Dictionary> getPagingData(int offset, int limit,String orderBy, Dictionary dictionary) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<Dictionary> pageInfo =new PageInfo<>(dictionaryMapper.list(dictionary));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(Dictionary dictionary){
        if(null!=dictionary.getUid()){
            this.update(dictionary);
        }else{
            this.insert(dictionary);
        }
        return dictionary.getUid();
    }
            }
